package com.basic.controller;

import com.code.domain.AjaxResult;
import com.code.domain.CommonResult;
import com.code.entity.User;
import com.code.util.RSAUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@ApiOperation(value = "登录模块",notes = "登录模块")
@Controller
@RequestMapping(value = "/basic/login")
@Slf4j
@CrossOrigin
public class LoginBasicController {

    @ApiOperation(value = "登录",notes = "登录")
    @RequestMapping(value = "/toSignin",method = RequestMethod.POST)
    public String toSignin(Model model, User user){
        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();
         try{
             //封装用户的登录信息
             UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),user.getPassword());
             //执行登录的方法，如果没有异常说明ok了
            subject.login(token);
            return "redirect:/";
        }catch (UnknownAccountException e){ // 用户名不存在
            model.addAttribute("msg","用户名错误");
            return "login/login";
        }catch (IncorrectCredentialsException e){ // 密码错误
            model.addAttribute("msg","密码错误");
            return "login/login";
        }catch (AccountException e){ // 密码错误
            model.addAttribute("msg","帐号或密码不正确！");
            return "login/login";
        }
    }

    @ApiOperation(value = "登录",notes = "登录")
    @RequestMapping(value = "/toAjaxSignin",method = RequestMethod.POST)
    public @ResponseBody
    AjaxResult toAjaxSignin(Model model, User user) throws Exception {
        //加密
        String password  = RSAUtil.getMd5(user.getPassword().trim());
        //获取当前的用户
        Subject subject = SecurityUtils.getSubject();
        try{
            //封装用户的登录信息
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(),password);
            //执行登录的方法，如果没有异常说明ok了
            subject.login(token);
            return  AjaxResult.success("登录成功",token);
        }catch (UnknownAccountException e){ // 用户名不存在
            return AjaxResult.error("登录失败","用户名错误");
        }catch (IncorrectCredentialsException e){ // 密码错误
            return AjaxResult.error("登录失败","密码错误");
        }catch (AccountException e){ // 密码错误
            return AjaxResult.error("登录失败","帐号或密码不正确！");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @ApiOperation(value = "退出登录",notes = "退出登录")
    @RequestMapping(value = "/logOut")
    public String logOut(Model model){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/";
    }

    @ApiOperation(value = "退出登录",notes = "退出登录")
    @RequestMapping(value = "/ajaxLogOut")
    public @ResponseBody AjaxResult ajaxLogOut(Model model){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return AjaxResult.success("登出成功");
    }

}
